import React, { Component, Fragment } from 'react';
import { connect } from 'dva'
import BaseTable from './baseTable'
import { Space, Popconfirm, Upload, Descriptions, Modal } from 'antd'
import constant from '../../constant/constant'

class TestTechnologyTable extends BaseTable {
    constructor(props) {
        super(props),
            this.state = {
                column: [
                    {
                        title: "技术编号", dataIndex: "id", key: "id",
                        render: (text, record, index) => {
                            return (
                                <p>{record.verificationTechnology.id}</p>
                            )
                        },
                        ...this.getColumnSearchProps("id")
                    },
                    { title: "技术名称", dataIndex: "technologyName", key: "technologyName",...this.getColumnSearchProps("technologyname") },
                    { title: "测试开始日期", dataIndex: "createdAt", key: "createdAt" },
                    {
                        title: "测试协议", dataIndex: "testAgreement", key: "testAgreement",
                        render: (text, record, index) => {
                            if (record.testAgreement  == null) {
                                return (
                                    <Upload
                                        action={constant.serverUrl + "/tvs/api/attachments/testfile/universalupload"}
                                        data={{
                                            id: record.id,
                                            mechanismId: this.props.user.mechanismId
                                        }}
                                        onChange={this.onChangeUpload}
                                        name={"testAgreement"}
                                        onprogress={false}
                                        showUploadList={false}
                                    >
                                        <a>上传测试协议</a>
                                    </Upload>
                                )
                            } else {
                                return (
                                    <Space>
                                        <a>{record.testAgreement}</a>
                                        <Upload
                                            action={constant.serverUrl + "/tvs/api/attachments/testfile/universalupload"}
                                            data={{
                                                id: record.id,
                                                mechanismId: this.props.user.mechanismId
                                            }}
                                            onChange={this.onChangeUpload}
                                            name={"testAgreement"}
                                            onprogress={false}
                                            onlist={false}
                                            showUploadList={false}
                                        >
                                            <a>修改</a>
                                        </Upload>
                                    </Space>
                                )
                            }
                        }
                    },
                    {
                        title: "测试数据", dataIndex: "testData", key: "testData",
                        render: (text, record, index) => {
                            if (record.testAgreement == null) {
                                return(
                                    "请先上传测试协议"
                                )
                            }
                            if (record.testAgreement != null) {
                                return (
                                    <Space>
                                        {record.testData  == null ? <Upload
                                            action={constant.serverUrl + "/tvs/api/attachments/testfile/universalupload"}
                                            data={{
                                                id: record.id,
                                                mechanismId: this.props.user.mechanismId
                                            }}
                                            onChange={this.onChangeUpload}
                                            name={"testData"}
                                            onprogress={false}
                                            showUploadList={false}
                                        >
                                            <a>上传测试数据</a>
                                        </Upload> : 
                                        <Space>
                                        <a>{record.testData}</a>
                                        <Upload
                                            action={constant.serverUrl + "/tvs/api/attachments/testfile/universalupload"}
                                            data={{
                                                id: record.id,
                                                mechanismId: this.props.user.mechanismId
                                            }}
                                            onChange={this.onChangeUpload}
                                            name={"testData"}
                                            onprogress={false}
                                            showUploadList={false}
                                        >
                                            <a>修改</a>
                                        </Upload>
                                        </Space>}
                                    </Space>
                                )
                            }
                        }
                    },
                    {
                        title: "测试报告", dataIndex: "testReport", key: "testReport",
                        render: (text, record, index) => {
                            if (record.testAgreement == null) {
                                return(
                                    "请先上传测试协议"
                                )
                            }
                            if (record.testAgreement != null) {
                                return (
                                    <Space>
                                        {record.testReport  == null ? <Upload
                                            action={constant.serverUrl + "/tvs/api/attachments/testfile/universalupload"}
                                            data={{
                                                id: record.id,
                                                mechanismId: this.props.user.mechanismId
                                            }}
                                            onChange={this.onChangeUpload}
                                            name={"testReport"}
                                            onprogress={false}
                                            showUploadList={false}
                                        >
                                            <a>上传测试报告</a>
                                        </Upload> : 
                                        <Space>
                                        <a>{record.testReport}</a>
                                        <Upload
                                            action={constant.serverUrl + "/tvs/api/attachments/testfile/universalupload"}
                                            data={{
                                                id: record.id,
                                                mechanismId: this.props.user.mechanismId
                                            }}
                                            onChange={this.onChangeUpload}
                                            name={"testReport"}
                                            onprogress={false}
                                            showUploadList={false}
                                        >
                                            <a>修改</a>
                                        </Upload>
                                        </Space>}
                                    </Space>
                                )
                            }
                        }
                    },
                ]
            }
    }

    onChangeUpload = info => {
        if (info.file.status == "done") {
            if (info.file.response.code == 200) {
                this.props.dispatch({
                    type: "mechanismOperation/getTestTechnologyTableDataByMechanismId",
                    data: {
                        mechanismId: this.props.user.mechanismId
                    }
                })

                this.props.dispatch({
                    type: "app/setServerRes",
                    data: info.file.response
                })
            }
        }
    }

    componentDidMount(){
        this.props.dispatch({
            type:"mechanismOperation/getTestTechnologyTableDataByMechanismId",
            data:{
                mechanismId:this.props.user.mechanismId
            }
        })
    }

    render(){
        return(
           <Fragment>
              {super.render(this.state.column, this.props.dataSource,{pageSize:12})}
           </Fragment>
        )
    }
}

const mapStateToProps = state => {
    let user = state.app.user
    let dataSource = state.mechanismOperation.testTechnologyList
    return{
        user,
        dataSource
    }
}

export default connect(mapStateToProps)(TestTechnologyTable)